package com.sunday.volunteer.utils;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.common.widgets.banner.holder.Holder;
import com.sunday.common.widgets.banner.holder.ViewHolderCreator;
import com.sunday.member.ui.WebViewActivity;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Images;
import com.sunday.volunteer.ui.MainActivity;

import java.util.List;


/**
 * Created by Pual's PC on 2016/10/9.
 */
public class ViewPagerUtils {
    private Context mContext;
    private TextView textView;
    public ViewPagerUtils(Context mContext, ConvenientBanner viewPager , List<Images> list , int height ,boolean boo){
        this.mContext = mContext;

        viewPager.setPages(new ViewHolderCreator<MyView>() {
            @Override
            public MyView createHolder() {
                return new MyView();
            }
        }, list);
        if(viewPager.isTurning()){
            return;
        }
        if(!boo){
            viewPager.setLayoutParams(new LinearLayout.LayoutParams(DeviceUtils.getDisplay(mContext).widthPixels , height));
        }


        viewPager.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
        viewPager.setPageIndicator(new int[]{R.drawable.point_false_white, R.drawable.point_true_white});
        viewPager.setPointViewVisible(true);
        viewPager.startTurning(2500);
    }

    public ViewPagerUtils(Context mContext, ConvenientBanner viewPager , List<String> list){
        this.mContext = mContext;

        viewPager.setPages(new ViewHolderCreator<MyView2>() {
            @Override
            public MyView2 createHolder() {
                return new MyView2();
            }
        }, list);
        if(viewPager.isTurning()){
            return;
        }
        //viewPager.setLayoutParams(new RelativeLayout.LayoutParams(DeviceUtils.getDisplay(mContext).widthPixels , height));

        viewPager.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
        viewPager.setPageIndicator(new int[]{R.drawable.point_false_white, R.drawable.point_true_white});
        viewPager.setPointViewVisible(true);
        viewPager.startTurning(2500);
    }



    public class MyView implements Holder<Images> {
        private ImageView imageView;
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, final Images data) {

            Picasso.with(imageView.getContext()).load(data.getImage()).placeholder(R.drawable.default_error).into(imageView);
            if(textView != null){
                textView.setText(data.getImage());
            }
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(TextUtils.isEmpty(data.getUrl())){
                        BaseUtil.openActivityDetialActivity(mContext , data.getActivityId());
                        return;
                    }
                    BaseUtil.openWebViewActivity(mContext , data.getUrl() , "志愿者");

                }
            });
        }
    }

    public class MyView2 implements Holder<String> {
        private ImageView imageView;
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, final String data) {

            Picasso.with(imageView.getContext()).load(data).placeholder(R.drawable.default_error).into(imageView);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseUtil.openBigImage(mContext , data);
                }
            });
        }
    }

}
